package com.white.ipcprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

public class BookProvider extends ContentProvider
{
    private static final String authority = "com.white.ipcprovider.bookprovider";

//    private static final String BOOK_URL = "content://"+authority+"/book";
//    private static final String USER_URL = "content://"+authority+"/user";

    private static final int BOOK_CODE = 0;
    private static final int USER_CODE = 1;

    private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    static{
        matcher.addURI(authority, "book", BOOK_CODE);
        matcher.addURI(authority, "user", USER_CODE);
    }

    public BookProvider()
    {
    }

    @Override
    public int delete(@NonNull Uri uri, String selection, String[] selectionArgs)
    {
        Log.e("s", "调用delete");
        String tableName = getTableNameFromUri(uri);
        if(!TextUtils.isEmpty(tableName))
        {
            int raw = mDb.delete(tableName, selection, selectionArgs);
            if(raw > 0)
            {
                mContext.getContentResolver().notifyChange(uri, null);
                return raw;
            }
        }
        return 0;
    }

    @Override
    public String getType(@NonNull Uri uri)
    {
        Log.e("s", "调用getType");
        return null;
    }

    @Override
    public Uri insert(@NonNull Uri uri, ContentValues values)
    {
        Log.e("s", "调用insert");
        String tableName = getTableNameFromUri(uri);
        if(!TextUtils.isEmpty(tableName))
        {
            mDb.insert(tableName, null,values);
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return uri;
    }

    Context mContext;
    SQLiteDatabase mDb;

    @Override
    public boolean onCreate()
    {
        Log.e("s", "onCreate tn = " + Thread.currentThread().getName());
        mContext = getContext();
        mDb = new BookDatabaseHelper(mContext).getWritableDatabase();
        initDb();
        return true;
    }

    private void initDb()
    {
        mDb.execSQL("delete from "+BookDatabaseHelper.BOOK_TB);
        mDb.execSQL("delete from "+BookDatabaseHelper.USER_TB);

        mDb.execSQL("insert into " + BookDatabaseHelper.BOOK_TB + " values(1, '安卓开发艺术探索I', 12.3);");
        mDb.execSQL("insert into " + BookDatabaseHelper.BOOK_TB + " values(2, '安卓开发艺术探索II', 22.3);");

        mDb.execSQL("insert into " + BookDatabaseHelper.USER_TB + " values(1, '路人甲', 23);");
        mDb.execSQL("insert into " + BookDatabaseHelper.USER_TB + " values(2, '路人乙', 25);");
        Log.e("s", "创建数据表");
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder)
    {
        Log.e("s", "调用query");
        String tableName = getTableNameFromUri(uri);
        Log.e("s", "查询出来的表名为：" + tableName + " uri = " + uri);
        if(!TextUtils.isEmpty(tableName))
        {
            return mDb.query(tableName, projection, selection, selectionArgs, null, sortOrder, null);
        }
        return null;
    }

    private String getTableNameFromUri(Uri uri)
    {
        if(uri != null)
        {
            int code = matcher.match(uri);
            switch (code)
            {
                case 0:
                    return BookDatabaseHelper.BOOK_TB;
                case 1:
                    return BookDatabaseHelper.USER_TB;
            }
            Log.e("s", "code = " + code);
        }
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs)
    {
        Log.e("s", "调用update");
        String tableName = getTableNameFromUri(uri);
        if(!TextUtils.isEmpty(tableName))
        {
            int raw = mDb.update(tableName,values, selection, selectionArgs);
            if(raw > 0)
            {
                mContext.getContentResolver().notifyChange(uri, null);
                return raw;
            }
        }
        return 0;
    }
}
